<?php
class WPaymentPagseguroIpn extends UWidgetWorklet
{
	public function accessRules()
	{
		return array(array('allow','users'=>array('*')));
	}
	
	public function taskConfig()
	{
		if(app()->request->isSecureConnection)
			wm()->get('base.init')->requireSecure = true;
		wm()->get('base.init')->setState('subscribe',false);
		
		if(count($_POST))
		{
			include_once(dirname(__FILE__).'/../components/UPagseguro.php');
			$p = new UPagseguro(array('token' => $this->param('token')));
			$validStatuses = array('Completo','Aprovado'/*,'Em Análise'*/);
			$status = iconv('ISO-8859-1','UTF-8',$_POST['StatusTransacao']);
			if($p->verify() && in_array($status,$validStatuses))
				wm()->get('payment.order')->charge($p->data['Referencia'],$p->data['TransacaoID']);
		}
		else
			app()->request->redirect(url('/payment/success'));
	}
}